﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class StudentInfo : System.Web.UI.Page
    {
        static int classid = 0;
        static int studentid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newStudent = from s in db.StudentInfo
                                  select s;
                    this.Store2.DataSource = newStudent;
                    this.Store2.DataBind();
                    var newClass = from c in db.ClassInfo join s in db.SchoolInfo on c.SchoolID equals s.SchoolID
                                   select new {SchoolID=c.SchoolID,ClassID=c.ClassID,FullName=s.FullName,ClassCode=c.ClassCode,ClassName=c.ClassName };
                    this.Store1.DataSource = newClass;
                    this.Store1.DataBind();
                    this.Store3.DataSource = newClass;
                    this.Store3.DataBind();
                }
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                classid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myClass = from c in db.ClassInfo
                              where c.ClassID == classid
                              select c.ClassName;
                foreach (var Class in myClass)
                {
                    DropDownField1.Text = Class.ToString();
                }
                var newStudent = from s in db.StudentInfo
                              where s.ClassID==classid
                              select s;
                this.Store2.DataSource = newStudent;
                this.Store2.DataBind();
            }
            classid = 0;
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            StudentCode.Text = "";
            StudentName.Text = "";
            StudentType.Text = "";
            AccountID.Text = "";
            Sex.Text = "";
            DropDownField2.Text = "请选择";
            this.FormWindow.Visible = true;
            this.FormWindow.Show();
            Button6.Show();
            Button7.Hide();
        }
        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    studentid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newstudent = from s in db.StudentInfo
                                   where s.StudentID==studentid
                                   select s;
                    ExamAnalysis.Model.StudentInfo news = newstudent.First();
                    var Class = from c in db.ClassInfo
                                 where c.ClassID == news.ClassID
                                 select c.ClassName;
                    classid = news.ClassID;
                    StudentCode.Text = news.StudentCode;
                    StudentName.Text=news.StudentName;
                    StudentType.Text=news.StudentType;
                    AccountID.Text = news.AccountID;
                    Sex.Text = news.Sex;
                    DropDownField2.Text = Class.First().ToString();
                }
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Hide();
                Button7.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个班级").Show();
            }
        }
        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if(sm.SelectedRows.Count!=0)
            {
            foreach (SelectedRow row in sm.SelectedRows)
            {
                studentid = Convert.ToInt32(row.RecordID);
            }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newstudent = from s in db.StudentInfo
                                   where s.StudentID == studentid
                                   select s;
                    ExamAnalysis.Model.StudentInfo news = newstudent.First();
                    db.StudentInfo.DeleteOnSubmit(news);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("StudentInfo.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个学生").Show();
            }
        }

        protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                classid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myClass = from c in db.ClassInfo
                              where c.ClassID == classid
                              select c.ClassName;
                foreach (var Class in myClass)
                {
                    DropDownField2.Text = Class.ToString();
                }
            }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if(DropDownField2.Text.ToString().Equals("请选择"))
            {
                X.Msg.Alert("提示信息", String.Format("请选择一个班级")).Show();
            }
            else
            {
            ExamAnalysis.Model.StudentInfo news = new ExamAnalysis.Model.StudentInfo();
            news.StudentCode = StudentCode.Text;
            news.StudentName = StudentName.Text;
            news.StudentType = StudentType.Text;
            news.AccountID = AccountID.Text;
            news.Sex = Sex.Text;
            news.ClassID = classid;
            news.IsValid = true;
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                db.StudentInfo.InsertOnSubmit(news);
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功添加学生")).Show();
                classid = 0;
            }
            Response.Redirect("StudentInfo.aspx");
            }
        }

        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newstudent = from s in db.StudentInfo
                                 where s.StudentID == studentid
                                 select s;
                ExamAnalysis.Model.StudentInfo news = newstudent.First();
                news.StudentCode = StudentCode.Text;
                news.StudentName = StudentName.Text;
                news.StudentType = StudentType.Text;
                news.AccountID = AccountID.Text;
                news.Sex = Sex.Text;
                news.ClassID = classid;
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改学生信息")).Show();
                classid = 0;
                studentid = 0;
            }
            Response.Redirect("StudentInfo.aspx");
        }
    }
}